package com.jiaai.cloud.base.util;

import com.alibaba.fastjson.JSON;
import com.jiaai.cloud.wx.entity.TWxshop;

import java.text.DecimalFormat;
import java.util.*;

/**
 * Created by Administrator on 2017/8/23.
 */
public class LatLngDistance {
    private static double EARTH_RADIUS = 6378.137;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 通过经纬度获取距离(单位：米)
     * @param lat1
     * @param lng1
     * @param lat2
     * @param lng2
     * @return
     */
    public static double getDistance(double lat1, double lng1, double lat2,
                                     double lng2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000d) / 10000d;
        DecimalFormat df = new DecimalFormat("#.##");

        return Double.parseDouble(df.format(s));
    }

    public static void main(String[] args) {
        //116.402754,39.913972
        //113.766578,23.046156
//        System.out.println(getDistance(22.98,113.99,23.020546,113.751778));//
//        JSON.toJSONString(new TWxshop());
//        List list = new ArrayList();
//        for (int i = 0; i < 100; i++) {
//            list.add(100 * new Random(i*100).nextDouble());
//        }
//        System.out.println(list);
//
//        list.sort((o1, o2)->{
//            //return Double.compare((double)o1, (double)o2);
//            double d1 = (double) o1;
//            double d2 = (double) o2;
//            if(d1 > d2){
//                return 1;
//            }
//            return -1;
//        });
//
//        System.out.println(list);


//        Timer timer = new Timer();
//        timer.schedule(new TimerTask() {
//            public void run() {
//                Key.k = 1;
//            }
//        }, 2000 , 5000);
//
//        for (; Key.k < 2000000000; Key.k++){
//            System.out.println(Key.k);
//        }
//    }
//
//    static class Key{
//        public static int k = 1;
//    }
//        DecimalFormat df = new DecimalFormat("#.##");
//
//        System.out.println(Double.parseDouble(df.format(1231.3654)));

    }
}
